package com.ooolong.expression.function;

import com.ooolong.expression.ExpressionException;
import com.ooolong.expression.OObject;
import com.ooolong.expression.function.string.CharAt;

public abstract class Function extends OObject {

	public static final String CHAR_AT = "CharAt";

	private String name;
	protected int argLength;
	protected int[] argTypeArray;
	protected OObject[] args;

	public static Function createFunction(String functionName) {
		if (functionName.equals(CHAR_AT)) {
			return new CharAt();
		}
		return null;
	}

	protected Function(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	protected void checkArguments(OObject[] args) throws ExpressionException {
		if (args.length != argLength) {
			throw new ExpressionException();
		}

		for (int i = 0; i < args.length; i++) {
			OObject arg = args[i];
			if (arg == null) {
				throw new ExpressionException();
			}
			if (arg.getType() != argTypeArray[i]) {
				throw new ExpressionException();
			}
		}
	}

	abstract public OObject execute(OObject[] args) throws ExpressionException;
}
